//
//  TTTableCustomMessageItemCell.m
//  UADE-Mobile
//
//  Created by Marcos Damian Pianelli on 11/22/11.
//  Copyright (c) 2011 DG. All rights reserved.
//

#import "TTTableCustomMessageItemCell.h"

#import "Three20UI/TTTableSubtextItemCell.h"

// UI
#import "Three20UI/TTTableSubtextItem.h"
#import "Three20UI/UITableViewAdditions.h"
#import "Three20UI/UIViewAdditions.h"

// Style
#import "Three20Style/TTGlobalStyle.h"
#import "Three20Style/TTDefaultStyleSheet.h"


static const NSInteger  kMessageTextLineCount       = 2;
static const CGFloat    kDefaultMessageImageWidth   = 34.0f;
static const CGFloat    kDefaultMessageImageHeight  = 34.0f;



@implementation TTTableCustomMessageItemCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)identifier {
	self = [super initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
    if (self) {
       /* self.textLabel.font = TTSTYLEVAR(font);
        self.textLabel.textColor = TTSTYLEVAR(textColor);
        self.textLabel.highlightedTextColor = TTSTYLEVAR(highlightedTextColor);
        self.textLabel.backgroundColor = TTSTYLEVAR(backgroundTextColor);
        self.textLabel.textAlignment = UITextAlignmentLeft;
        self.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
        self.textLabel.adjustsFontSizeToFitWidth = YES;
        self.textLabel.contentMode = UIViewContentModeLeft;
        
        self.detailTextLabel.font = TTSTYLEVAR(font);
        self.detailTextLabel.textColor = TTSTYLEVAR(tableSubTextColor);
        self.detailTextLabel.highlightedTextColor = TTSTYLEVAR(highlightedTextColor);
        self.detailTextLabel.backgroundColor = TTSTYLEVAR(backgroundTextColor);
        self.detailTextLabel.textAlignment = UITextAlignmentLeft;
        self.detailTextLabel.contentMode = UIViewContentModeTop;
        self.detailTextLabel.lineBreakMode = UILineBreakModeTailTruncation;*/
        self.detailTextLabel.numberOfLines = 0;

    }
    
    return self;
}




+ (CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(id)object {
    TTTableSubtextItem* item = object;
    
    CGFloat width = tableView.width - [tableView tableCellMargin]*2 - kTableCellHPadding*2;
    
    CGSize detailTextSize = [item.text sizeWithFont:TTSTYLEVAR(tableFont)
                                  constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                                      lineBreakMode:UILineBreakModeWordWrap];
    
    CGSize textSize = [item.caption sizeWithFont:TTSTYLEVAR(font)
                               constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                                   lineBreakMode:UILineBreakModeWordWrap];
    
    return kTableCellVPadding*4 + detailTextSize.height + textSize.height;
    
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat left = 0.0f;
    if (_imageView2) {
        _imageView2.frame = CGRectMake(kTableCellSmallMargin, kTableCellSmallMargin,
                                       kDefaultMessageImageWidth, kDefaultMessageImageHeight);
        left += kTableCellSmallMargin + kDefaultMessageImageHeight + kTableCellSmallMargin;
        
    } else {
        left = kTableCellMargin;
    }
    
    CGFloat width = self.contentView.width - left;
    CGFloat top = kTableCellSmallMargin;
    
    if (_titleLabel.text.length) {
        _titleLabel.frame = CGRectMake(left, top, width, _titleLabel.font.lineHeight);
        top += _titleLabel.height;
        
    } else {
        _titleLabel.frame = CGRectZero;
    }
    
    if (self.captionLabel.text.length) {
        self.captionLabel.frame = CGRectMake(left, top, width, self.captionLabel.font.lineHeight);
        top += self.captionLabel.height;
        
    } else {
        self.captionLabel.frame = CGRectZero;
    }
   

    

    if (self.detailTextLabel.text.length) {
        CGFloat width = self.contentView.width;
        
        CGSize detailTextSize = [self.detailTextLabel.text sizeWithFont:TTSTYLEVAR(tableFont)
                                                      constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                                                          lineBreakMode:UILineBreakModeWordWrap];
        self.detailTextLabel.frame = CGRectMake(left, top, width, detailTextSize.height);
        
    } else {
        self.detailTextLabel.frame = CGRectZero;
    }
    
 /*   if (_timestampLabel.text.length) {
        _timestampLabel.alpha = !self.showingDeleteConfirmation;
        [_timestampLabel sizeToFit];
        _timestampLabel.left = self.contentView.width - (_timestampLabel.width + kTableCellSmallMargin);
        _timestampLabel.top = _titleLabel.top;
        _titleLabel.width -= _timestampLabel.width + kTableCellSmallMargin*2;
        
    } else {
        _timestampLabel.frame = CGRectZero;
    }*/
}





@end
